package Manager.Account;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.*;
import GenericClass.*;

public class PanelAccount extends ManagementPanel
{
	private JTable tableComplement;
	private JScrollPane scrolltableComplement;
	private String[] tabHeaderDescript = {"Course"};
	private FormatScrollPane formatScrollPane;
	private JLabel labelAccountName, labelBoundCourses, labelBoundClasses;
	private ArrayList<String> arrayHeader = new ArrayList<String>();
	
	public PanelAccount(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection);
		
		formatScrollPane = new FormatScrollPane();
		
		createComponent();
		showComponent();
	}
	
	public void createComponent()
	{
		// Redefinition de la taille du table descript
		scrolltableDescript.setBounds(410, 57, 330, 150);
		
		// JLabel
		labelAccountName = new JLabel("Account name");
		labelAccountName.setFont(new Font("Candara", 0, 15));
		labelAccountName.setBounds(55,30,165,27);
		labelAccountName.setForeground(Color.WHITE);
		
		labelBoundCourses = new JLabel("Bound courses");
		labelBoundCourses.setFont(new Font("Candara", 0, 15));
		labelBoundCourses.setBounds(415,30,165,27);
		labelBoundCourses.setForeground(Color.WHITE);
		
		labelBoundClasses = new JLabel("Bound classes");
		labelBoundClasses.setFont(new Font("Candara", 0, 15));
		labelBoundClasses.setBounds(415,233,165,27);
		labelBoundClasses.setForeground(Color.WHITE);
		
		// JTable
		tableComplement = new JTable();
		scrolltableComplement = new JScrollPane();
		scrolltableComplement.setBounds(410, 261, 330, 150);
		
		MyMouseListener mouseListener = new MyMouseListener();
		
		tableLeft.addMouseListener(mouseListener);
	}
	
	public void showComponent()
	{
		this.add(labelAccountName);
		this.add(labelBoundCourses);
		this.add(labelBoundClasses);
		this.add(scrolltableComplement);
		
		GraphicsSetting.setBackground(this, "image/Bg-Main.jpg", 0, 0, 800, 600);
	}
	
	public void deleteAccount() throws LoadDriverException, EraseDataException, InsertionDataException, CheckDataException
	{
		new AccountMgmt(connection).deleteElement((int)GeneralInformation.getMyAccountModel().getValueAt(tableLeft.getSelectedRow(), 2));
	}
	
	private class MyMouseListener implements MouseListener
	{
		public void mouseClicked(MouseEvent arg0)
		{
			try
			{
				int selectedRow = (tableLeft.getSelectedRow());
				
				//Creation de la JTable de droite (haut)
				GeneralInformation.createAccountBoundCourses(connection, tableLeft, selectedRow);
				GeneralMethod.setDescriptModel(GeneralInformation.getBoundCourseDescriptModel());
				refreshSuperDescriptTable();
				
				//Creation de la JTable de droite (bas)
				MyTableModel tableModel = new AccountMgmt(connection).selectBoundClasses((Integer)tableLeft.getValueAt(selectedRow, 2));
				
				String[] tabHeader = {"Year", "Section", "Group"};
				for(String item : tabHeader)
					arrayHeader.add(item);
				
				formatScrollPane.autoFillScrollPane(tableComplement, tableModel, arrayHeader, scrolltableComplement, "single", true, true, true, false);
			}
			catch(CheckDataException | FormatScrollPaneException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
		
		public void mouseEntered(MouseEvent arg0){}
		public void mouseExited(MouseEvent arg0){}
		public void mousePressed(MouseEvent arg0){}
		public void mouseReleased(MouseEvent arg0){}
	}

	public JTable getTable(){return tableLeft;}
}